package com.huawei.appmarket.oobe.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.webkit.ConsoleMessage;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.huawei.appmarket.R;
import com.huawei.appmarket.service.webview.js.HwHiAppPrivacyJs;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.ACTION;
import com.huawei.secure.android.common.intent.SafeIntent;
import java.util.Locale;
import java.util.regex.Pattern;
import o.ayy;
import o.dlf;
import o.dli;
import o.Logger;
import o.eee;
import o.eef;

public class OOBEProtocolActivity extends Activity {
    private boolean ˊ;
    private WebView ˎ;
    private dlf ˏ;

    static class a implements OnLongClickListener {
        a() {
        }

        public boolean onLongClick(View view) {
            return true;
        }
    }

    class d extends WebChromeClient {
        final /* synthetic */ OOBEProtocolActivity ˊ;

        d(OOBEProtocolActivity oOBEProtocolActivity) {
            this.ˊ = oOBEProtocolActivity;
        }

        public void onProgressChanged(WebView webView, int i) {
            super.onProgressChanged(webView, i);
            if (i == 100 && this.ˊ.ˊ) {
                this.ˊ.ˊ = false;
                this.ˊ.ॱ();
                Logger.ˎ("OOBE", "OOBEProtocolActivity onProgressChanged.hideLoading");
            }
        }

        public void onReceivedTitle(WebView webView, String str) {
            super.onReceivedTitle(webView, str);
            Logger.ˎ("OOBE", "OOBEProtocolActivity onReceivedTitle:" + str);
            if (this.ˊ.ॱ(str) || TextUtils.isEmpty(str)) {
                str = webView.getContext().getString(R.string.client_app_name);
            }
            ActionBar actionBar = this.ˊ.getActionBar();
            if (actionBar != null) {
                actionBar.setTitle(str);
                Logger.ˎ("OOBE", "OOBEProtocolActivity onReceivedTitle.setTitle");
            }
        }

        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
            return true;
        }
    }

    class e extends WebViewClient {
        final /* synthetic */ OOBEProtocolActivity ॱ;

        e(OOBEProtocolActivity oOBEProtocolActivity) {
            this.ॱ = oOBEProtocolActivity;
        }

        public void onPageStarted(WebView webView, String str, Bitmap bitmap) {
            super.onPageStarted(webView, str, bitmap);
            this.ॱ.ˎ.setVisibility(0);
            this.ॱ.ˏ();
            Logger.ˎ("OOBE", "OOBEProtocolActivity onPageStarted.startLoading");
        }

        public void onPageFinished(WebView webView, String str) {
            super.onPageFinished(webView, str);
            Logger.ˎ("OOBE", "OOBEProtocolActivity onPageFinished");
            this.ॱ.ˊ = false;
        }

        public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
            super.onReceivedError(webView, webResourceRequest, webResourceError);
            this.ॱ.ˊ = false;
            this.ॱ.ˎ.setVisibility(4);
            if (this.ॱ.ˏ != null) {
                this.ॱ.ˏ.ॱ(0);
                this.ॱ.ˏ.ˋ();
                Logger.ˎ("OOBE", "OOBEProtocolActivity stopLoading");
            }
        }

        public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
            this.ॱ.ˊ = false;
            sslErrorHandler.cancel();
            this.ॱ.ˎ.setVisibility(4);
            if (this.ॱ.ˏ != null) {
                this.ॱ.ˏ.ॱ(0);
                this.ॱ.ˏ.ˋ();
                Logger.ˎ("OOBE", "OOBEProtocolActivity onReceivedSslError.stopLoading");
            }
        }

        public boolean shouldOverrideUrlLoading(WebView webView, String str) {
            if (dli.ˏ(this.ॱ)) {
                if (!TextUtils.isEmpty(str) && str.toLowerCase(Locale.ENGLISH).startsWith("http")) {
                    this.ॱ.ˏ(str);
                }
            } else if (!TextUtils.isEmpty(str)) {
                try {
                    Intent intent = new Intent(ACTION.HWID_SCHEME_URL, Uri.parse(str));
                    intent.setFlags(268435456);
                    this.ॱ.startActivity(intent);
                } catch (Exception e) {
                    Logger.ˋ("OOBE", "shouldOverrideUrlLoading exception");
                }
            }
            return true;
        }
    }

    public static void ॱ(Context context) {
        Intent intent = new Intent(context, OOBEProtocolActivity.class);
        intent.setAction("com.huawei.appmarket.oobe.ACTION_SHOW_PRIVACY");
        intent.putExtra("protocolType", 0);
        context.startActivity(intent);
    }

    public static void ˋ(Context context) {
        Intent intent = new Intent(context, OOBEProtocolActivity.class);
        intent.setAction("com.huawei.appmarket.oobe.ACTION_SHOW_PRIVACY");
        intent.putExtra("protocolType", 1);
        context.startActivity(intent);
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        ayy.ˋ().ˎ(getWindow());
        Intent safeIntent = new SafeIntent(getIntent());
        if (safeIntent.getAction() != null && safeIntent.getAction().equals("com.huawei.appmarket.oobe.ACTION_SHOW_PRIVACY")) {
            getActionBar().setHomeButtonEnabled(true);
            getActionBar().setDisplayHomeAsUpEnabled(true);
            setContentView(R.layout.activity_oobeprotocol);
            this.ˎ = (WebView) findViewById(R.id.webview);
            ˋ();
            int intExtra = safeIntent.getIntExtra("protocolType", 0);
            Object ˋ = ˋ(intExtra);
            if (TextUtils.isEmpty(ˋ)) {
                Logger.e("OOBE", "protocol url is null");
                finish();
                return;
            }
            ॱ(intExtra);
            this.ˎ.loadUrl(ˋ);
        }
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != 16908332) {
            return super.onOptionsItemSelected(menuItem);
        }
        finish();
        return true;
    }

    private String ˋ(int i) {
        if (i == 0) {
            return new eef().ˎ();
        }
        if (i == 1) {
            return new eef().ˊ();
        }
        return "";
    }

    private void ॱ(int i) {
        HwHiAppPrivacyJs hwHiAppPrivacyJs = new HwHiAppPrivacyJs(this);
        this.ˎ.addJavascriptInterface(hwHiAppPrivacyJs, HwHiAppPrivacyJs.JS_AGRATTR_NAME);
        if (eee.ˊ() && i == 0) {
            this.ˎ.addJavascriptInterface(hwHiAppPrivacyJs, HwHiAppPrivacyJs.JS_CHECK_MORE_NAME);
        }
    }

    private void ˋ() {
        WebSettings settings = this.ˎ.getSettings();
        settings.setCacheMode(1);
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setDomStorageEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUserAgentString(settings.getUserAgentString() + " hispace");
        settings.setAllowFileAccessFromFileURLs(false);
        settings.setAllowUniversalAccessFromFileURLs(false);
        settings.setAllowFileAccess(false);
        settings.setAllowContentAccess(false);
        settings.setRenderPriority(RenderPriority.HIGH);
        settings.setSavePassword(false);
        this.ˎ.removeJavascriptInterface("accessibility");
        this.ˎ.removeJavascriptInterface("accessibilityTraversal");
        this.ˎ.removeJavascriptInterface("searchBoxJavaBridge_");
        this.ˎ.setOnLongClickListener(new a());
        this.ˎ.setLayerType(2, null);
        this.ˎ.setLongClickable(false);
        this.ˎ.setHapticFeedbackEnabled(false);
        this.ˎ.setWebViewClient(new e(this));
        this.ˎ.setWebChromeClient(new d(this));
    }

    private void ˏ(String str) {
        new Builder(this).setMessage(str).setCancelable(true).setNeutralButton(R.string.exit_confirm, null).show();
    }

    private void ˏ() {
        this.ˊ = true;
        if (this.ˏ == null) {
            this.ˏ = new dlf();
            this.ˏ.ˏ(findViewById(R.id.oobe_protocol_loadingPage));
            this.ˏ.ˊ();
        }
        new Handler().postDelayed(new Runnable(this) {
            final /* synthetic */ OOBEProtocolActivity ˏ;

            {
                this.ˏ = r1;
            }

            public void run() {
                if (this.ˏ.ˏ != null && this.ˏ.ˊ) {
                    this.ˏ.ˏ.ॱ(0);
                }
            }
        }, 600);
    }

    private void ॱ() {
        if (this.ˏ != null) {
            this.ˏ.ॱ(8);
            this.ˏ = null;
        }
        this.ˎ.setVisibility(0);
    }

    private boolean ॱ(String str) {
        try {
            return Pattern.compile("^(http://|https://)", 2).matcher(str).find();
        } catch (Exception e) {
            Logger.e("OOBEProtocolActivity", "catch a Exception");
            return false;
        }
    }
}
